import {
    Dispatch
} from 'redux';

import ajax from './../../http';

import * as types from './../types/video';

import getUserId from './../../utils/userId';

interface LimitPagePayload {
    lastId: number | string,
    isPush?: boolean,
    limit?: number
}

interface VideoPayload extends LimitPagePayload {
    searchType: 'hot' | 'newlest'
}

interface VideoPara extends LimitPagePayload{
    videoId:any
}

export default {
    //获取视频列表
    [types.getAllVideoList](payload: VideoPayload) {
        return async (dispatch: Dispatch) => {
            try {
                const {
                    lastId,
                    isPush,
                    searchType
                } = payload;

                const _para = {
                    limit: 10,
                    lastId: lastId,
                    searchType: searchType
                }

                const list = (
                    await ajax.get(
                        `video/list`,
                        _para
                    )
                )

                const _data = {
                    queryList: list,
                    isPush: isPush,
                    userId: getUserId(),
                    searchType: searchType
                }

                dispatch({
                    data: _data,
                    type: types.getAllVideoList
                })

                return list;
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //获取我喜欢过得视频
    [types.getUserLikedVideo](payload: VideoPayload) {
        return async (dispatch: Dispatch) => {
            const {
                lastId = 0,
                searchType,
                isPush = false
            } = payload;
            try {
                const _para = {
                    limit: 20,
                    lastId: lastId
                }
                const {
                    list
                } = await ajax.get(
                    `video/like/list`, _para
                )
                const _data = {
                    queryList: list,
                    isPush: isPush,
                    userId: getUserId(),
                    searchType: searchType
                }
                dispatch({
                    data: _data,
                    type: types.getAllVideoList
                })
                return list;
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    },
    //获取当前视频有哪些用户喜欢
    [types.getVideoLikeUsers](payload:VideoPara) {
        return async (dispatch: Dispatch) => {
            const {
                limit=40,
                videoId,
                lastId
            } = payload;

            const _para = {
                limit,
                videoId,
                lastId
            }

            try {
                const list = await ajax.get(
                    `video/like/users`, _para
                )
                const _data = {
                    queryList:list,
                    isPush:!!lastId,
                    videoId:videoId
                }
                dispatch({
                    data:_data,
                    type:types.getVideoLikeUsers
                })
                return list;
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //获取当前视频有哪些评论
    [types.getVideoComments](payload:VideoPara) {
        return async (dispatch: Dispatch) => {
            const {
                limit=40,
                videoId,
                lastId
            } = payload;

            const _para = {
                limit,
                videoId,
                lastId
            }

            try {
                const list = await ajax.get(
                    `video/comment/list`, _para
                )
                const _data = {
                    queryList:list,
                    isPush:!!lastId,
                    videoId:videoId
                }
                dispatch({
                    data:_data,
                    type:types.getVideoComments
                })
                return list;
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
}